﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Model.Raw_Materials
{
    [Serializable]
    public abstract class Raw : INotifyPropertyChanged, ISerializable
    {

        //protected String _Name;
        protected double _Quality;
        protected double _Quantity;
        protected double _Price;
        protected String _BaseName;
        protected String _Description;

        public virtual String Name { get; set; }

        public Raw() { }

        protected Raw(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");
            BaseName = (String)info.GetValue("BaseName", typeof(String));
            Quantity = (double)info.GetValue("Quantity", typeof(double));
            Quality = (double)info.GetValue("Quality", typeof(double));            
            Price = (double)info.GetValue("Price", typeof(double));
            Name = (String)info.GetValue("Name", typeof(String));
        }

        public Raw(String basename)
        {
            BaseName = basename;
        }

        public Raw(String basename, double quality, double quantity, double price)
        {
            BaseName = basename;
            Quality = quality;
            Quantity = quantity;
            Price = price;
        }

        public String BaseName //the basic name "inherited" by anything that is crafted from it
        {
            get { return _BaseName; }
            set
            {
                _BaseName = value;
                NotifyPropertyChanged("BaseName");
                NotifyPropertyChanged("Name"); //in child classes (no idea if it works like that!)
            }
        }

        public double Quantity
        {
            get { return _Quantity; }
            set
            {
                _Quantity = value;
                NotifyPropertyChanged("Quantity");
            }
        }

        public double Quality
        {
            get { return _Quality; }
            set
            {
                _Quality = value;
                NotifyPropertyChanged("Quality");
            }
        }

        public double Price
        {
            get { return _Price; }
            set
            {
                _Price = value;
                NotifyPropertyChanged("Price");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public override string ToString()
        {
            return "\nQuantity: " + Quantity + "\nQuality " + Quality;
        }

        [SecurityPermission(SecurityAction.LinkDemand,
        Flags = SecurityPermissionFlag.SerializationFormatter)]
        public virtual void GetObjectData(
        SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");
            //Console.WriteLine("GetObjectData in Raw");
            info.AddValue("Name", Name);
            info.AddValue("BaseName", BaseName);
            info.AddValue("Quantity", Quantity);
            info.AddValue("Quality", Quality);
            info.AddValue("Price", Price);
        }
    }
}
